<template>
	<view class="container">
		<!-- 空白页 -->
		<view v-if="!hasLogin || empty===true" class="empty">
			<image v-if="hasLogin" style="width: 240rpx; height: 240rpx;"
				src="https://qiniu.dadaozhixing.top/dxx/static/not_msg.png" mode="aspectFit"></image>
			<image v-else style="width: 240rpx; height: 240rpx;"
				src="https://qiniu.dadaozhixing.top/dxx/static/not_login.png" mode="aspectFit"></image>
			<view v-if="hasLogin" class="empty-tips">
				<view class="">
					购物车空空如也，去逛逛吧
				</view>
				<view class="navigator" @click="navToLogin(1)">去逛逛</view>
			</view>
			<view v-else class="empty-tips">
				<view class="">
					请先登录
				</view>
				<view class="navigator" @click="navToLogin(1)">去登录</view>
			</view>
		</view>
		<view v-else>
			<view class="goods-list">
				<block v-for="(item, index) in cartList" :key="item.id">
					<view class="goods-item" :class="{'b-b': index!==cartList.length-1}">
						<view class="selected" @click="chooseItem(item)">
							<image v-if="item.checked || allChecked"
								src="https://qiniu.dadaozhixing.top/dxx/static/select_icon.png" mode=""></image>
						</view>
						<view class="image-wrapper">
							<image :src="item.goodsSize.icon || item.goodsSize.goods.icon" mode="aspectFill"></image>
						</view>
						<view class="text-box">
							<text class="title clamp">{{item.goodsSize.name}}</text>
							<text class="sub-title clamp">{{item.goodsSize.name}}</text>
							<view class="price-box">
								<view>
									<text class="price">{{item.goodsSize.unitPrice}}</text>
									<uni-number-box class="uni-number-box" :value="item.buyNumber" @change="changeBuyNumber($event,index)"
										:min="0"></uni-number-box>
								</view>

							</view>
						</view>
					</view>
				</block>
			</view>
			<view class="submit-box">
				<view class="all-select-box">
					<view class="all-check-box" @click="changeAllChecked">
						<image v-if="allChecked" src="https://qiniu.dadaozhixing.top/dxx/static/select_icon.png" mode="">
						</image>
					</view>
					<span style="padding-left: 8upx;">全选</span>
				</view>

				<view class="total-price">
					<span>合计:</span>
					<span class="price">￥{{curTotalPrice}}</span>
					<button type="primary" class="no-border confirm-btn" @click="createOrder">结算</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import uniNumberBox from '@/components/uni-number-box.vue'
	import {
		cartGoodsSizeListApi,updateCartApi
	}
	from "@/api/goods";
	export default {
		components: {
			uniNumberBox
		},
		data() {
			return {
				total: 0, //总价格
				allChecked: false, //全选状态  true|false
				empty: false, //空白页现实  true|false
				cartList: [],
				buyNumberList: [],
				defaultBuyNumber: 1,
				totalPrice: 0,
				buyNumber: 3
			};
		},
		onLoad() {

		},
		onShow() {
			this.loadData();
		},
		watch: {
			//显示空白页
			cartList(e) {
				let empty = e.length === 0 ? true : false;
				if (this.empty !== empty) {
					this.empty = empty;
				}
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
			curTotalPrice() {
				let price = 0
				this.cartList.forEach(item => {
					if (item.checked) {
						price = price + item.unitPrice * item.buyNumber
					}
				})
				return this.$api.toFixed(price)
			}
		},
		methods: {
			//请求数据
			async loadData() {
				if (this.hasLogin) {
					let ret = await cartGoodsSizeListApi({
						id: this.userInfo.id
					})
					if (ret && ret.code == 200) {
						ret.data.forEach(item => {
							item.checked = false
						})
						this.cartList = ret.data
						this.buyNumberList = ret.data.map(x=>x.buyNumber)
					}
				}
			},
			//监听image加载完成
			onImageLoad(key, index) {
				this.$set(this[key][index], 'loaded', 'loaded');
			},
			//监听image加载失败
			onImageError(key, index) {
				this[key][index].image = 'https://qiniu.dadaozhixing.top/dxx/errorImage.jpg';
			},
			navToLogin(index) {
				if (index == 1) {
					uni.switchTab({
						url: '/pages/index/index'
					})
				} else {
					uni.navigateTo({
						url: '/pages/public/login'
					})
				}

			},
			chooseItem(item) {
				console.log(item);
				console.log(item.checked, item.id);
				item.checked = !item.checked
				this.allChecked = false
			},
			changeAllChecked(event) {
				this.allChecked = !this.allChecked
				this.cartList.forEach(item => {
					item.checked = this.allChecked
				})
			},
			//选中状态处理
			check(type, index) {
				if (type === 'item') {
					this.cartList[index].checked = !this.cartList[index].checked;
				} else {
					const checked = !this.allChecked
					const list = this.cartList;
					list.forEach(item => {
						item.checked = checked;
					})
					this.allChecked = checked;
				}
				this.calcTotal(type);
			},
			//数量
			async changeBuyNumber(event,index) {
				let row = this.cartList[index]
				row.buyNumber = event
				console.log(row);
				let ret = await updateCartApi({id: row.id,goodsSizeId: row.goodsSize.id,buyNumber: event})
				if (event === 0) {
					// 删除当前商品
					this.$api.msg("已删除")
					this.cartList.splice(index,1)
				}
				if (ret && ret.code == 200) {
					this.loadData()
				}
				
				
			},
			//删除
			deleteCartItem(index) {
				let list = this.cartList;
				let row = list[index];
				let id = row.id;

				this.cartList.splice(index, 1);
				this.calcTotal();
				uni.hideLoading();
			},
			//清空
			clearCart() {
				uni.showModal({
					content: '清空购物车？',
					success: (e) => {
						if (e.confirm) {
							this.cartList = [];
						}
					}
				})
			},
			//计算总价
			calcTotal() {
				let list = this.cartList;
				if (list.length === 0) {
					this.empty = true;
					return;
				}
				let total = 0;
				let checked = true;
				list.forEach(item => {
					if (item.checked === true) {
						total += item.price * item.number;
					} else if (checked === true) {
						checked = false;
					}
				})
				this.allChecked = checked;
				this.total = Number(total.toFixed(2));
			},
			//创建订单
			createOrder() {
				let list = this.cartList;
				let goodsData = [];
				list.forEach(item => {
					if (item.checked) {
						goodsData.push({id: item.id,buyNumber: item.buyNumber,goodsSizeId: item.goodsSize.id,icon: item.goodsSize.icon || item.goodsSize.goods.icon,unitPrice: item.unitPrice})
					}
				})
				if (goodsData.length == 0) {
					return this.$api.msg("请选择商品")
				}
				uni.navigateTo({
					url: `/pages/order/createOrder?data=${JSON.stringify(goodsData)}&sense=cart`
				})
				this.$api.msg('跳转下一页 sendData');
			}
		}
	}
</script>

<style lang='scss' scoped>
	.container {
		position: relative;
		min-height: 100vh;
		padding: 24upx;
		background: #F5F5F9;

		/* 空白页 */
		.empty {
			position: fixed;
			left: 24upx;
			top: 24upx;
			width: 702upx;
			height: calc(100vh - 48upx);
			border-radius: 16rpx;
			display: flex;
			padding-top: 240rpx;
			flex-direction: column;
			align-items: center;
			background: #fff;

			image {
				width: 240upx;
				height: 160upx;
				margin-bottom: 30upx;
			}

			.empty-tips {
				font-size: 28rpx;
				color: #7F808A;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.navigator {
					width: 176rpx;
					height: 64rpx;
					text-align: center;
					line-height: 64rpx;
					font-size: 28rpx;
					color: #fff;
					background: #3EB3F3;
					border-radius: 32rpx;
					margin-top: 32rpx;
				}
			}
		}
	}

	/* 购物车列表项 */
	.cart-item {
		display: flex;
		position: relative;
		padding: 30upx 40upx;

		.image-wrapper {
			width: 170rpx;
			height: 170rpx;
			flex-shrink: 0;
			position: relative;

			image {
				border-radius: 8upx;
			}
		}

		.checkbox {
			position: absolute;
			left: -16upx;
			top: -16upx;
			z-index: 8;
			font-size: 44upx;
			line-height: 1;
			padding: 4upx;
			color: $font-color-disabled;
			background: #fff;
			border-radius: 50px;
		}

		.item-right {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow: hidden;
			position: relative;
			padding-left: 30upx;

			.title,
			.price {
				font-size: $font-base + 2upx;
				color: $font-color-dark;
				height: 40upx;
				line-height: 40upx;
			}

			.attr {
				font-size: $font-sm + 2upx;
				color: $font-color-light;
				height: 50upx;
				line-height: 50upx;
			}

			.price {
				height: 50upx;
				line-height: 50upx;
			}
		}

		.del-btn {
			padding: 4upx 10upx;
			font-size: 34upx;
			height: 50upx;
			color: $font-color-light;
		}
	}

	/* 底部栏 */
	.action-section {
		/* #ifdef H5 */
		margin-bottom: 100upx;
		/* #endif */
		position: fixed;
		left: 30upx;
		bottom: 30upx;
		z-index: 95;
		display: flex;
		align-items: center;
		width: 690upx;
		height: 100upx;
		padding: 0 30upx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
		border-radius: 16upx;

		.checkbox {
			height: 52upx;
			position: relative;

			image {
				width: 52upx;
				height: 100%;
				position: relative;
				z-index: 5;
			}
		}

		.clear-btn {
			position: absolute;
			left: 26upx;
			top: 0;
			z-index: 4;
			width: 0;
			height: 52upx;
			line-height: 52upx;
			padding-left: 38upx;
			font-size: $font-base;
			color: #fff;
			background: $font-color-disabled;
			border-radius: 0 50px 50px 0;
			opacity: 0;
			transition: .2s;

			&.show {
				opacity: 1;
				width: 120upx;
			}
		}

		.total-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			text-align: right;
			padding-right: 40upx;

			.price {
				font-size: $font-lg;
				color: $font-color-dark;
			}

			.coupon {
				font-size: $font-sm;
				color: $font-color-light;

				text {
					color: $font-color-dark;
				}
			}
		}

		
	}

	/* 复选框选中状态 */
	.action-section .checkbox.checked,
	.cart-item .checkbox.checked {
		color: $uni-color-primary;
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;

		.goods-item {
			margin: 24upx 0;
			display: flex;
			background: #fff;
			align-items: center;
			height: 228upx;
			width: 100%;
			border-radius: 16upx;
		}

		.selected {
			width: 36rpx;
			height: 36rpx;
			border: 1upx solid #C9C9C9;
			margin: 0 32rpx;
			border-radius: 50%;

			image {
				width: 100%;
				height: 100%;
			}

		}

		.image-wrapper {
			width: 170upx;
			height: 170upx;
			overflow: hidden;
			margin-right: 16rpx;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.text-box {
			display: flex;
			flex-direction: column;

			.sub-title {
				font-size: 24rpx;
				color: #7F808A;
				line-height: 36rpx;
			}
		}

		.title {
			font-weight: 600;
			font-weight: bold;
			font-size: 28rpx;
			color: #23242E;
			line-height: 42rpx;
		}

		.price-box {
			margin-top: 52upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;

			.uni-number-box {
				position: relative;
			}
		}

		.price {
			font-size: 36rpx;
			color: #FF5C5C;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 36upx;
			}
		}

		.add-to-cart {
			margin-left: 250upx;
			width: 36upx;
			height: 36upx;
		}
	}

	.submit-box {
		position: absolute;
		left: 0;
		bottom: 0;
		width: 750upx;
		height: 96upx;
		line-height: 96upx;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.all-select-box {
			display: flex;
			align-items: center;
			padding-left: 24upx;
			font-size: 28rpx;
			color: #23242E;
		}
		

		.total-price {
			display: flex;
			align-items: center;
			.price {
				min-width: 170rpx;
				font-weight: 600;
				font-size: 44rpx;
				color: #FF5C5C;
			}
			.confirm-btn {
				width: 160rpx;
				height: 64rpx;
				margin-right: 24upx;
				line-height: 64upx;
				color: white;
				background: #3EB3F3;
				border-radius: 32rpx;
			}
		}
	}
	.all-check-box {
		width: 38rpx;
		height: 38rpx;
		border: 1rpx solid #C9C9C9;
		border-radius: 50%;
		display: flex;
	
		image {
			width: 38upx!important;
			height: 38upx;
		}
	}
</style>